package AuxComp;

import Lexer.*;
import java.io.*;
import java.util.*;

public class CompilationErrorList {

    public CompilationErrorList( Lexer lexer ) {
          // output of an error is done in out
        this.lexer = lexer;
        thereWasAnError = false;
        errorList = new ArrayList<CompilationError>();
    }

    public void setLexer( Lexer lexer ) {
        this.lexer = lexer;
    }

    public boolean wasAnErrorSignalled() {
        return thereWasAnError;
    }

    public void addWarning( String strMessage ) {
        addWarning( strMessage, false );
    }

    public void addWarning( String strMessage, boolean goPreviousToken ) {
        // is goPreviousToken is true, the error is signalled at the line of the
        // previous token, not the last one.

    	CompilationError ce;

        if ( goPreviousToken ) {
        	ce = new CompilationError(strMessage,
        			lexer.getLineNumberBeforeLastToken(),
        				lexer.getColumnNumber(),
        				lexer.getLineBeforeLastToken() );

        }
        else {
        	ce = new CompilationError(strMessage,
        			    lexer.getLineNumber(),
        				lexer.getColumnNumber(),
        				lexer.getCurrentLine() );

        }
        /*
        out.println( strMessage );
        out.flush();
        if ( out.checkError() )
          System.out.println("Error in signaling an error");
        */
        thereWasAnError = true;
        errorList.add(ce);
    }

    public void addError( String strMessage ) {
        addWarning( strMessage );
        throw new RuntimeException();
    }

    public void print(PrintWriter out) {
    	for ( CompilationError ce : errorList )
    		ce.print(out);
    	out.flush();
    }


	public ArrayList<CompilationError> getErrorList() {
		return errorList;
	}

	public int size() {
		return errorList.size();
	}

	private Lexer lexer;
    private boolean thereWasAnError;
    private ArrayList<CompilationError> errorList;
}

